# coding=utf-8
import sys
import random
import codecs


random.seed(222)

exp_file = []
# exp_file.append('fire_data/running/conv-s2s-d0.3_bpe500/40hyp.txt.1.topk40.t1.0')
exp_file.append('fire_data/running/conv-s2s-d0.3-fusion_bpe500/18hyp.txt.1.topk20.t1.0')
# exp_file.append('fire_data/running/transformer-lm_bpe500_warmlr_format2/40hyp.txt.1.topk40.t1.0')
exp_file.append('fire_data/running/transformer-lm_bpe500_warmlr_format_pre2/27hyp.txt.1.topk20.t1.0')
exp_file.append('fire_data/running/bilstm_transformer-lm_bpe500_format_pre_srl_hier4_fix/25hyp.txt.own.prompt2srl2_500_wo_smooth_d1.1.topk20.t1.0')
exp_file.append('out/bilstm_transformer-lm_bpe500_format_pre_srl_hier_pos_fix/17hyp.txt.own.prompt2srl2_500_wo_smooth_d1_verb_loss.1.topk20.t1.0')


base_path = '/home/rickwwang/project_research/'
surfix_path = '.human'
mid_path = exp_file[0]
m1_path = base_path + mid_path + surfix_path
mid_path = exp_file[1]
m2_path = base_path + mid_path + surfix_path
mid_path = exp_file[2]
m3_path = base_path + mid_path + surfix_path
mid_path = exp_file[3]
m4_path = base_path + mid_path + surfix_path

# m1_path = sys.argv[1]
# m2_path = sys.argv[2]
# m3_path = sys.argv[3]
# m4_path = sys.argv[4]
result_path = '/home/rickwwang/project_research/fairseq/data-bin/all_story.txt'

m1_file = codecs.open(m1_path, 'r', encoding='utf8')
m2_file = codecs.open(m2_path, 'r', encoding='utf8')
m3_file = codecs.open(m3_path, 'r', encoding='utf8')
m4_file = codecs.open(m4_path, 'r', encoding='utf8')

m1_items = m1_file.readlines()
m2_items = m2_file.readlines()
m3_items = m3_file.readlines()
m4_items = m4_file.readlines()

topics = m4_items[::2]
m1_items = m1_items[1::2]
m2_items = m2_items[1::2]
m3_items = m3_items[1::2]
m4_items = m4_items[1::2]


def convert(x):
    x = [x[i].strip() + '\t' + str(i) for i in range(len(x))]
    # random.shuffle(x)
    return '\n'.join(x)


zip_all = zip(m1_items, m2_items, m3_items, m4_items)
print(len(zip_all))
items = map(convert, zip_all)
final = zip(topics, items)
final = map(lambda x: ''.join(x), final)

# write to file
with codecs.open(result_path, 'w', encoding='utf8') as fout:
    fout.write('\n'.join(final))
    fout.write('\n')
